//
//  UIViewController+CenterAlertView.m
//  HX_GTS2
//
//  Created by GC_tandy on 2018/8/13.
//  Copyright © 2018年 gw. All rights reserved.
//

#import "UIViewController+CenterAlertView.h"
#import "UIColor+add.h"


@implementation UIViewController (CenterAlertView)



+ (void)showCenterAlertViewWithTitle:(NSString *)title detailMsg:(NSString *)msg leftBtnTitle:(NSString *)leftTitle leftBtnAction:(void(^)(UIAlertAction *alertAction))leftClickAction rightBtnTitle:(NSString *)rightTitle rightBtnAction:(void(^)(UIAlertAction *alertAction))rightClickAction{
    
    UIViewController *rootvc = [UIApplication sharedApplication].delegate.window.rootViewController;
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:title message:msg preferredStyle:UIAlertControllerStyleAlert];
    
    UIColor *titleColor = HexColor(@"#333333");
    UIColor *messageColor = HexColor(@"#888888");
    
    
    //修改title
    NSMutableAttributedString *alertControllerStr = [[NSMutableAttributedString alloc] initWithString:title];
    [alertControllerStr addAttribute:NSForegroundColorAttributeName value:titleColor range:NSMakeRange(0, title.length)];
    [alertControllerStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:15] range:NSMakeRange(0, title.length)];
    [alertVC setValue:alertControllerStr forKey:@"attributedTitle"];
    
    //修改message
    NSMutableAttributedString *alertControllerMessageStr = [[NSMutableAttributedString alloc] initWithString:msg];
    [alertControllerMessageStr addAttribute:NSForegroundColorAttributeName value:messageColor range:NSMakeRange(0, msg.length)];
    [alertControllerMessageStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:13] range:NSMakeRange(0, msg.length)];
    [alertVC setValue:alertControllerMessageStr forKey:@"attributedMessage"];
    
    
    
    if(leftTitle){
        UIAlertAction *action_Left = [UIAlertAction actionWithTitle:leftTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            if(leftClickAction){
                leftClickAction(action);
            }
        }];
        [action_Left setValue: HexColor(@"#F8A916") forKey:@"titleTextColor"];
        [alertVC addAction:action_Left];
    }
    if(rightTitle){
        UIAlertAction *action_Right = [UIAlertAction actionWithTitle:rightTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            if(rightClickAction){
                rightClickAction(action);
            }
        }];
        [action_Right setValue: HexColor(@"#F8A916") forKey:@"titleTextColor"];
        [alertVC addAction:action_Right];
    }
    [rootvc presentViewController:alertVC animated:YES completion:nil];
}


- (void)showCenterAlertViewWithTitle:(NSString *)title detailMsg:(NSString *)msg leftBtnTitle:(NSString *)leftTitle leftBtnAction:(void(^)(UIAlertAction *alertAction))leftClickAction rightBtnTitle:(NSString *)rightTitle rightBtnAction:(void(^)(UIAlertAction *alertAction))rightClickAction{
   
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:title message:msg preferredStyle:UIAlertControllerStyleAlert];
    
    UIColor *titleColor = HexColor(@"#333333");
    UIColor *messageColor = HexColor(@"#888888");
    
    
    //修改title
    NSMutableAttributedString *alertControllerStr = [[NSMutableAttributedString alloc] initWithString:title];
    [alertControllerStr addAttribute:NSForegroundColorAttributeName value:titleColor range:NSMakeRange(0, title.length)];
    [alertControllerStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:15] range:NSMakeRange(0, title.length)];
    [alertVC setValue:alertControllerStr forKey:@"attributedTitle"];
    
    //修改message
    NSMutableAttributedString *alertControllerMessageStr = [[NSMutableAttributedString alloc] initWithString:msg];
    [alertControllerMessageStr addAttribute:NSForegroundColorAttributeName value:messageColor range:NSMakeRange(0, msg.length)];
    [alertControllerMessageStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:13] range:NSMakeRange(0, msg.length)];
    [alertVC setValue:alertControllerMessageStr forKey:@"attributedMessage"];
    
    
    
    if(leftTitle){
        UIAlertAction *action_Left = [UIAlertAction actionWithTitle:leftTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            if(leftClickAction){
                leftClickAction(action);
            }
        }];
        [action_Left setValue: HexColor(@"#F8A916") forKey:@"titleTextColor"];
        [alertVC addAction:action_Left];
    }
    if(rightTitle){
        UIAlertAction *action_Right = [UIAlertAction actionWithTitle:rightTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            if(rightClickAction){
                rightClickAction(action);
            }
        }];
        [action_Right setValue: HexColor(@"#F8A916") forKey:@"titleTextColor"];
        [alertVC addAction:action_Right];
    }
    [self presentViewController:alertVC animated:YES completion:nil];
}



@end
